﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using QLNS.Models;
using DTO;
namespace QLNS.api
{
    public class NgoaiNguAPIController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<NGOAINGU> Get()
        {
            return new WebData().NGOAINGUs.GetAll();
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public HttpResponseMessage Post(NGOAINGU value)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var model = new WebData();
                    model.NGOAINGUs.InsertOnSubmit(ref value);
                    var response = Request.CreateResponse<NGOAINGU>(HttpStatusCode.Created, value);
                    return response;
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid");
                }
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, NGOAINGU item)
        {
            try
            {
                WebData model = new WebData();
                model.NGOAINGUs.UpdateOnSubmit(item);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        // DELETE api/<controller>/5
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                WebData model = new WebData();
                NGOAINGU item = new NGOAINGU();
                item.ID = id;
                model.NGOAINGUs.DeleteOnSubmit(item);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}